<?php

include_once(APP_URL.'protected/myUtil/CheckUtil.php');
class UserController extends Controller {
    
    public function actionPostRegMsg() {
        // http://112.74.203.16/wifishare_server/index.php?r=user/postregmsg
        
        // check param
        include_once(APP_URL.'protected/myUtil/RegxUtil.php');
        $userPhone = RegxUtil::regxPhone(110102, 'mobilePhone');
        
        // phone registered
        $conditionArr = array(
            'select' => array('user_id'),
            'condition' => 'user_phone=:userPhone',
            'params' => array('userPhone'=>$userPhone));// fill condition
        $TblUser = new TblUser();
        if ($TblUser->selectRecord($conditionArr)) {// phone registered
            echo EchoUtil::retFailed(110103, 'phone registered !');
            exit;
        }

        // update record or add record
        $usertVericode = rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9);
        $TblUsert = new TblUsert();
        include_once(APP_URL.'protected/myUtil/HelperUtil.php');
        $conditionArr = array(
            'select' => array('*'),
            'condition' => 'usert_phone=:userPhone',
            'params' => array('userPhone'=>$userPhone));// fill condition
        $result = $TblUsert->selectRecord($conditionArr);
        if ($result) {// phone exsit
            if (time() - $result->usert_time <= SEND_VERIFYCODE_INTERVAL) {// 60s
                echo EchoUtil::retFailed(110104, 'interval time less than 60s !');
                exit;
            }
            if (!HelperUtil::sendSms($userPhone, $usertVericode)) {// send verifycode msg
                echo EchoUtil::retFailed(110101, 'send msg failed !');
                exit;
            }
            $conditionArr = array(
                'condition' => 'usert_phone=:userPhone',
                'params' => array('userPhone'=>$userPhone)
            );// fill condition
            $contentArr = array(
                'usert_vericode' => $usertVericode,
                'usert_time' => time()
            );// fill content
            if (!$TblUsert->updateRecord($conditionArr, $contentArr)) {// update record
                echo EchoUtil::retFailed(110101, 'server error !');
                exit;
            };
        } else {// phone not exsit
            if (!HelperUtil::sendSms($userPhone, $usertVericode)) {// send verifycode msg
                echo EchoUtil::retFailed(110101, 'send msg failed !');
                exit;
            }
            $contentArr = array(
                'usert_phone' => $userPhone,
                'usert_vericode' => $usertVericode,
                'usert_time' => time()
            );// fill content
            if (!$TblUsert->addRecord('usert', $contentArr)) {// add record
                echo EchoUtil::retFailed(110101, 'server error !');
                exit;
            };
        }
        
	    echo EchoUtil::msgSuccess(1, (object)array());// return success
	    
    }
    
    public function actionPostReg() {
        // http://112.74.203.16/wifishare_server/index.php?r=user/postreg
        
        // check param
        include_once(APP_URL.'protected/myUtil/RegxUtil.php');
        $userPhone = RegxUtil::regxPhone(110201, 'userPhone');
        $userName = RegxUtil::regxUsername(110102, 'userName');
        $userPwd = RegxUtil::regxPassword(110201, 'userPassword');
        $vericode = RegxUtil::regxVerifycode(110201, 'verifyCode');

        // phone registered
        $conditionArr = array(
            'select' => array('user_id'),
            'condition' => 'user_phone=:userPhone',
            'params' => array('userPhone'=>$userPhone));// fill condition
        $TblUser = new TblUser();
        if ($TblUser->selectRecord($conditionArr)) {// phone registered
            echo EchoUtil::retFailed(110201, 'phone registered !');
            exit;
        }
        
        // check vericode
        $TblUsert = new TblUsert();
        $conditionArr = array(
            'select' => array('*'),
            'condition' => 'usert_phone=:userPhone',
            'params' => array('userPhone'=>$userPhone));// fill condition
        $result = $TblUsert->selectRecord($conditionArr);
        if (!$result) {
            echo EchoUtil::retFailed(110202, 'vericode error !');
            exit;
        }
        if ($result->usert_vericode != $vericode) {
            echo EchoUtil::retFailed(110202, 'vericode error !');
            exit;
        }
        
        // add record
        $userSession = md5($userPhone.microtime());
        $contentArr = array(
            'user_phone' => $userPhone,
            'user_session' => $userSession,
            'user_name' => $userName,
            'user_pwd' => $userPwd
        );// fill content
        $userId = $TblUser->addRecord('user', $contentArr);
        if (!$userId) {
            echo EchoUtil::retFailed(110201, 'server error !');
            exit;
        }
        $conditionArr = array(
            'condition' => 'usert_phone=:userPhone',
            'params' => array('userPhone'=>$userPhone));// fill condition
        if (!$TblUsert->deleteRecord($conditionArr)) {
            echo EchoUtil::retFailed(110201, ' server error !');
            exit;
        }
        
        // return success
        echo EchoUtil::msgSuccess(1, array(
            'userId'=>$userId,
            'session'=>$userSession
        ));
        
    }
    
    public function actionPostLogin() {
        // http://112.74.203.16/wifishare_server/index.php?r=user/postlogin
        
        // check param
        include_once(APP_URL.'protected/myUtil/RegxUtil.php');
        $userPhone = RegxUtil::regxPhone(110301, 'userAccount');
        $userPwd = RegxUtil::regxPassword(110301, 'userPassword');

        // check pwd
        $conditionArr = array(
            'select' => array('user_id', 'user_session'),
            'condition' => 'user_phone=:userPhone AND user_pwd=:userPwd',
            'params' => array('userPhone'=>$userPhone, 'userPwd'=>$userPwd));// fill condition
        $TblUser = new TblUser();
        $result = $TblUser->selectRecord($conditionArr);
        if (!$result) {// not exist
            echo EchoUtil::retFailed(110302, 'account or password error !');
            exit;
        }
        
        // update record
        $userSession = md5($userPhone.microtime());
        $conditionArr = array(
            'condition' => 'user_id=:userId',
            'params' => array('userId'=>$result->user_id)
        );// fill condition
        $contentArr = array(
            'user_session' => $userSession
        );// fill content
        if (!$TblUser->updateRecord($conditionArr, $contentArr)) {// update session
            echo EchoUtil::retFailed(110301, 'server error !');
            exit;
        };
        
        // return success
        echo EchoUtil::msgSuccess(1, array(
            'userId'=>$result->user_id,
            'session'=>$userSession
        ));
        
    }
    

    public function actionPostPhoneMsg() {
        // http://112.74.203.16/wifishare_server/index.php?r=user/postphonemsg
    
        // check param
        include_once(APP_URL.'protected/myUtil/RegxUtil.php');
        $userPhone = RegxUtil::regxPhone(110401, 'userPhone');
        $type = CheckUtil::checkPostParam(110401, 'typeTag', false);
        
        // identify user
        $conditionArr = array(
            'select' => array('*'),
            'condition' => 'user_phone=:userPhone',
            'params' => array('userPhone'=>$userPhone));// fill condition
        $TblUser = new TblUser();
        $result = $TblUser->selectRecord($conditionArr);
        if (!$result) {// not exsit
            echo EchoUtil::retFailed(110402, 'user not exsit !');
            exit;
        }

        $vericode = rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9);
        $time = time();
        switch ($type) {
            case 1:
                if ($time - $result->user_pwd_time <= SEND_VERIFYCODE_INTERVAL) {// check time 60s
                    echo EchoUtil::retFailed(110401, 'msg interval time less than 60s !');
                    exit;
                }
                $contentArr = array(
                    'user_pwd_vc' => $vericode,
                    'user_pwd_time' => $time
                );// fill content
                break;
            case 2:
                if ($time - $result->user_paypwd_time <= SEND_VERIFYCODE_INTERVAL) {// check time 60s
                    echo EchoUtil::retFailed(110401, 'msg interval time less than 60s !');
                    exit;
                }
                $contentArr = array(
                    'user_paypwd_vc' => $vericode,
                    'user_paypwd_time' => $time
                );// fill content
                break;
             default:
                echo EchoUtil::retFailed(110401, 'typeTag error !');
                exit;
        }
        
        // send vericode msg
        include_once(APP_URL.'protected/myUtil/HelperUtil.php');
        if (!HelperUtil::sendSms($userPhone, $vericode)) {// send vericode msg
            echo EchoUtil::retFailed(110401, 'server error !');
            exit;
        }
        $conditionArr = array(
            'condition' => 'user_id=:userId',
            'params' => array('userId'=>$result->user_id)
        );// fill condition
        if (!$TblUser->updateRecord($conditionArr, $contentArr)) {// update record
            echo EchoUtil::retFailed(110401, 'post phone vericode msg failed !');
            exit;
        };

        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
         
    }


    public function actionPostPhoneToken() {
        // http://112.74.203.16/wifishare_server/index.php?r=user/postphonetoken
    
        // check param
        include_once(APP_URL.'protected/myUtil/RegxUtil.php');
        $userPhone = RegxUtil::regxPhone(110501, 'userPhone');
        $type = CheckUtil::checkPostParam(110501, 'typeTag', false);
        $vericode = RegxUtil::regxVerifycode(110501, 'verifyCode');
        
        // identify user
        $conditionArr = array(
            'select' => array('*'),
            'condition' => 'user_phone=:userPhone',
            'params' => array('userPhone'=>$userPhone));// fill condition
        $TblUser = new TblUser();
        $result = $TblUser->selectRecord($conditionArr);
        if (!$result) {// not exsit
            echo EchoUtil::retFailed(110501, 'user not exsit !');
            exit;
        }

        // update record
        $token = md5($userPhone.microtime());// generate token
        switch ($type) {
            case 1:// check pwd vericode
                if ($vericode != $result->user_pwd_vc) {
                    echo EchoUtil::retFailed(110502, 'vericode error !');
                    exit;
                }
                $contentArr = array(
                    'user_pwd_tk' => $token
                );// fill content
                break;
            case 2:
                if ($vericode != $result->user_paypwd_vc) {
                    echo EchoUtil::retFailed(110502, 'vericode error !');
                    exit;
                }
                $contentArr = array(
                    'user_paypwd_tk' => $token
                );// fill content
                break;
            default:
                echo EchoUtil::retFailed(110501, 'typeTag error !');
                exit;
        }
        $conditionArr = array(
            'condition' => 'user_id=:userId',
            'params' => array('userId'=>$result->user_id)
        );// fill condition
        if (!$TblUser->updateRecord($conditionArr, $contentArr)) {// update record
            echo EchoUtil::retFailed(110502, 'post phone token failed !');
            exit;
        };
        
        // return success
        echo EchoUtil::msgSuccess(1, array(
                'userId'=>$result->user_id,
                'token'=>$token
        ));
    
    }

    public function actionPostModpwd() {
        // http://112.74.203.16/wifishare_server/index.php?r=user/postmodpwd
    
        // check param
        $userId = CheckUtil::checkPostParam(110601, 'userId', false);
        $userPwd = CheckUtil::checkPostParam(110601, 'userPassword', false);
        $token = CheckUtil::checkPostParam(110301, 'token', false);
    
        // check account
        $conditionArr = array(
            'select' => array('*'),
            'condition' => 'user_id=:userId',
            'params' => array('userId'=>$userId));// fill condition
        $TblUser = new TblUser();
        $result = $TblUser->selectRecord($conditionArr);
        if (!$result) {// not exsit
            echo EchoUtil::retFailed(110601, 'user not exist !');
            exit;
        }

        if ($token != $result->user_pwd_tk) {
            echo EchoUtil::retFailed(110603, 'token error !');
            exit;
        }
        
        $conditionArr = array(
            'condition' => 'user_id=:userId',
            'params' => array('userId'=>$result->user_id)
        );// fill condition
        $contentArr = array(
            'user_pwd' => $userPwd
        );// fill content
        if (!$TblUser->updateRecord($conditionArr, $contentArr)) {// update record
            echo EchoUtil::retFailed(110601, 'server error !');
            exit;
        }
        
        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
        
    }
    
    public function actionPostModname() {
        // http://112.74.203.16/wifishare_server/index.php?r=user/postmodname
        
        // check param
        $userId = CheckUtil::checkPostParam(112201, 'userId', false);
        $userSession = CheckUtil::checkPostParam(112201, 'session', false);
        $userName = CheckUtil::checkPostParam(112201, 'userName', false);
    
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(112201, 'user not exsit !');
            exit;
        }
    
        // update record
        $conditionArr = array(
            'condition' => 'user_id=:userId',
            'params' => array('userId'=>$userId)
        );// fill condition
        $contentArr = array(
            'user_name' => $userName
        );// fill content
        if (!$result->updateRecord($conditionArr, $contentArr)) {// update record
            echo EchoUtil::retFailed(112202, 'mod name failed !');
            exit;
        }
        
        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
    
    }

    public function actionPostModPaypwd() {
        // http://112.74.203.16/wifishare_server/index.php?r=user/postmodpaypwd

        // check param
        $userId = CheckUtil::checkPostParam(111201, 'userId', false);
        $userPaypwd = CheckUtil::checkPostParam(111201, 'userPassword', false);
        $token = CheckUtil::checkPostParam(111201, 'token', false);
    
        // check token
        $conditionArr = array(
            'select' => array('*'),
            'condition' => 'user_id=:userId',
            'params' => array('userId'=>$userId)
        );// fill condition
        $TblUser = new TblUser();
        $result = $TblUser->selectRecord($conditionArr);
        if ($token != $result->user_paypwd_tk) {
            echo EchoUtil::retFailed(111203, 'token error !');
            exit;
        }

        // update record
        $conditionArr = array(
            'condition' => 'user_id=:userId',
            'params' => array('userId'=>$userId)
        );// fill condition
        $contentArr = array(
            'user_paypwd' => $userPaypwd
        );// fill content
        if (!$TblUser->updateRecord($conditionArr, $contentArr)) {// update record
            echo EchoUtil::retFailed(111202, 'mod paypwd failed !');
            exit;
        }
        
        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
        
    }

    public function actionGetInfo() {
        // http://112.74.203.16/wifishare_server/index.php?r=user/getinfo
    
        // param check
        $userId = CheckUtil::checkGetParam(100101, 'userId', false);
        $userSession = CheckUtil::checkGetParam(100101, 'session', false);
    
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(100101, 'user not exsit !');
            exit;
        }
    
        // select cont
        $userName = $result->user_name;
        $userRmoney = is_null($result->user_rmoney) ? "" : $result->user_rmoney;
        
        $TblPkg = new TblPkg();// select pkg type
        $conditionArr = array(
            'select' => array('pkg_type'),
            'condition' => 'pkg_uid=:pkgUid',
            'params' => array('pkgUid'=>$userId)
        );// fill condition
        $resultPkg = $TblPkg->selectRecord($conditionArr);
        $userPkgtype = "";
        if ($resultPkg) {
            $userPkgtype = $resultPkg->pkg_type;
        }
        
        $TblDevice = new TblDevice();// select device id
        $conditionArr = array(
            'select' => array('device_id'),
            'condition' => 'device_uid=:deviceUid',
            'params' => array('deviceUid'=>$userId)
        );// fill condition
        $resultDevice = $TblDevice->selectRecord($conditionArr);
        $userDid = "";
        if ($resultDevice) {
            $userDid = $resultDevice->device_id;
        }
        
        // return success
        echo EchoUtil::msgSuccess(1, array(
            'userName' => $userName,
            'userRemainMoney' => $userRmoney,
            'userPackageType' => $userPkgtype,
            'deviceId' => $userDid
        ));
         
    }
    
}
